package cn.zjitc.controller;

import cn.zjitc.constant.MessageConstant;
import cn.zjitc.entity.PageResult;
import cn.zjitc.entity.QueryPageBean;
import cn.zjitc.entity.Result;
import cn.zjitc.pojo.Book;
import cn.zjitc.pojo.User;
import cn.zjitc.service.AdminUserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 管理员界面操作
 */
@RestController
@RequestMapping("/admin")
public class AdminUserController {
    @Reference
    private AdminUserService adminUserService;

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user){
        try{
            adminUserService.add(user);
            adminUserService.addborrow(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增用户失败");
        }
        return new Result(true,"新增用户成功");
    }

    //删除用户
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            adminUserService.deleteByIdBorrow(id);
            adminUserService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
        return new Result(true,"删除用户成功");
    }

    //编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user){
        try{
            adminUserService.edit(user);
            adminUserService.editborrow(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑用户失败");
        }
        return new Result(true,"编辑用户成功");
    }

    //编辑用户信息
    @RequestMapping("/edituser")
    public Result edituser(@RequestBody User user){
        try{
            adminUserService.edituser(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑用户失败");
        }
        return new Result(true,"编辑用户成功");
    }

    //查询所有用户情况
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<User> list= adminUserService.findAll();
            return new Result(true, MessageConstant.ADMIN_USER_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADMIN_USER_FAIL);
        }
    }

    //根据用户id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            User user=adminUserService.findById(id);
            return new Result(true, MessageConstant.ADMIN_USER_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADMIN_USER_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=adminUserService.pageQuery(queryPageBean);
        return pageResult;
    }

    //根据用户id查询
    @RequestMapping("/findByName")
    public Result findByName(String name){
        try {
            User user=adminUserService.findByName(name);
            return new Result(true, MessageConstant.ADMIN_USER_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADMIN_USER_FAIL);
        }
    }
}
